<?php
namespace App\Services;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Storage;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Redis;
use Log,Exception,Config;
use App\Services\WechatoffiaccountService;
use App\Services\WechatService;
use Symfony\Component\Cache\Adapter\RedisAdapter;

class WechatJsSdkService
{
    public static function officialAccount()
    {
        $config = Config::get("weixin.offiaccount.cgsq");
        $app = Factory::officialAccount($config);
        $predis = app('redis')->connection()->client();
        $cache = new RedisAdapter($predis);
        $app->rebind('cache', $cache);
        return $app;
    }

    public static function officialAccessToken($forth)
    {
        $official = self::officialAccount();
        $accessToken = $official->access_token;
        if ($forth) {
            $token = $accessToken->getToken(true);
        } else {
            $token = $accessToken->getToken();
        }
        return $token;
    }

    public static function wechatShareParamas($links_id,$type,$channel,$link_type)
    {
        try {
            $accessToken = self::officialAccessToken(false);
        } catch (Exception $e) {
            $errormsg = $e->getMessage();
            $message = json_decode(str_replace("Request access_token fail: ", "", $errormsg));
            return ["status"=>"error","msg"=>"链接出现异常,请联系客服处理！"];
        }
        $config = config("weixin.offiaccount.cgsq");
        // 必须认证,成哥说球
        $appid = $config["app_id"]; //填入服务号AppID
        if (!isset($accessToken["access_token"])) return ["status"=>"error","msg"=>"接口异常,请稍后尝试！"];
        $access_token = $accessToken["access_token"];
        $tickets = WechatService::getTicket($appid,$access_token);
        if (!isset($tickets["ticket"])) return ["status"=>"error","msg"=>"接口异常,请稍后尝试！"];
        $ticket = $tickets["ticket"];
        $timestamp = time();

        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $nonceStr = "";
        for ($i = 0; $i < 16; $i++) {
            $nonceStr .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        if (!empty($channel)) {
            $url = "https://card.pdb2.com/card/".$links_id."?channel=".$channel."&type=".$type."&link_type=".$link_type; // 调用JSSDK的页面地址
        } else {
            $url = "https://card.pdb2.com/card/".$links_id."?type=".$type."&link_type=".$link_type; // 调用JSSDK的页面地址
        }
        $signature = sha1("jsapi_ticket={$ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}");
        return ["status"=>"success","paramas"=>["appid"=>$appid,"timestamp"=>$timestamp,"nonceStr"=>$nonceStr,"signature"=>$signature]];
    }

    public static function wechatJssdkParamas($weburl)
    {
        try {
            $accessToken = self::officialAccessToken(false);
        } catch (Exception $e) {
            $errormsg = $e->getMessage();
            $message = json_decode(str_replace("Request access_token fail: ", "", $errormsg));
            return ["status"=>"error","msg"=>"链接出现异常,请联系客服处理！"];
        }
        $config = config("weixin.offiaccount.cgsq");
        // 必须认证,天天外链
        $appid = $config["app_id"]; //填入服务号AppID
        if (!isset($accessToken["access_token"])) return ["status"=>"error","msg"=>"接口异常,请稍后尝试！"];
        $access_token = $accessToken["access_token"];
        $tickets = WechatService::getTicket($appid,$access_token);
        if (!isset($tickets["ticket"])) return ["status"=>"error","msg"=>"接口异常,请稍后尝试！"];
        $ticket = $tickets["ticket"];
        $timestamp = time();

        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $nonceStr = "";
        for ($i = 0; $i < 16; $i++) {
            $nonceStr .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        $signature = sha1("jsapi_ticket={$ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$weburl}");
        return ["status"=>"success","paramas"=>["appid"=>$appid,"timestamp"=>$timestamp,"nonceStr"=>$nonceStr,"signature"=>$signature]];
    }

}